package com.caesar.string;

import org.junit.jupiter.api.Test;

/**
 * @author caesar
 * @date 2025/7/15
 * @Description:
 * @apiNote
 */
public class TestStringBuilder {
    @Test
    public void test1(){
        StringBuilder builder = new StringBuilder();
        builder.append("hello");//拼接
        builder.append(1);
        builder.append(1.0);
        builder.append(true);
        System.out.println(builder);
    }

    @Test
    public void test2(){
        StringBuilder builder = new StringBuilder("hello");
        builder.insert(3, "java");//插入
        System.out.println(builder);
        //heljavalo
    }

    @Test
    public void test3(){
        StringBuilder builder = new StringBuilder("hellojavaworld");
        builder.deleteCharAt(0);//删除
        System.out.println(builder);//ellojavaworld

        builder.delete(0,3);//删除[0,3)
        System.out.println(builder);//ojavaworld
    }

    @Test
    public void test4(){
        StringBuilder builder = new StringBuilder("hellojavaworld");
        builder.setCharAt(0,'H');//首字母改为大写
        System.out.println(builder);//Hellojavaworld
        builder.reverse();//翻转
        System.out.println(builder);//dlrowavajolleH
        builder.replace(0,3,"尚硅谷");//替换
        System.out.println(builder);
        builder.setLength(5);
        System.out.println(builder);//尚硅谷ow

        builder.setLength(20);
        System.out.println(builder);//尚硅谷ow
    }

    @Test
    public void test5(){
        StringBuilder builder = new StringBuilder("hellojavaworldjavajava");
        int index = builder.indexOf("java");
        System.out.println(index);//5

        int lastIndex = builder.lastIndexOf("java");
        System.out.println(lastIndex);//18

        char c = builder.charAt(1);
        System.out.println(c);//e

        String sub = builder.substring(0, 3);
        System.out.println(sub);//hel
    }
}
